import Vue from "vue"
import VueRouter from "vue-router"

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: () => import("@/pages/login")
        },
        {
            path: "/1",
            component: () => import("@/pages/1"),
            children: [
                {
                    path: "/addArticle",
                    component: () => import("@/pages/addArticle")
                },
                {
                    path: "/index",
                    component: () => import("@/pages/index")
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (to.path == "/login") {
        next()
    } else {
        let token = sessionStorage.getItem("token")
        let time = sessionStorage.getItem("time")
        if (token) {
            if (new Date(Number(time)+60*1000)<new Date()){
                next("/login")
            }else{
                next()
            }
        } else {
            next('/login')
        }
    }
})



export default router